package com.daliu.service.impl;

import com.daliu.mapper.UserMapper;
import com.daliu.pojo.po.Role;
import com.daliu.pojo.po.User;
import com.daliu.pojo.vo.AddUser;
import com.daliu.pojo.vo.PageResult;
import com.daliu.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;




    @Override
    public List<Role> findALl() {
        return mapper.findALl();
    }

    /**
     * findByPage根据分页查询信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findByPage(int currentPage, int pageSize) {
        //根据传过来的信息,使用pageHelper来帮助我们查询
        PageHelper.startPage(currentPage, pageSize);
        //使用查询所有的方法,把所有的数据查询出来
        List<User> userList = mapper.findAllUser();
        //使用PageInfo方法
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        /*//pageHelper对于多对多联查不友好，还是用之前的查询方法
        int startNum = (currentPage-1)*pageSize;
        List<User> list = mapper.findByPage(startNum,pageSize);*/

        for (User user : userList) {
           List<Role> roles = mapper.findUserRoles(user.getId());
           user.setRoles(roles);
        }
        //得到查询后的分页信息

        //得到查询后的总条数
        long total = pageInfo.getTotal();
        return new PageResult(total, userList);
    }

    @Override
    public int addUser(AddUser addUser) {
        User user = new User();
        user.setUsername(addUser.getUsername());
        user.setEmail(addUser.getEmail());
        user.setEmail(addUser.getEmail());
        user.setRemark(addUser.getRemark());
        int num = mapper.addUser(user);
        if (addUser.getRoleIds().size()!= 0 && addUser.getRoleIds()!= null){
            for (String roleId : addUser.getRoleIds()) {
                mapper.addRole(roleId,user.getId());
            }
        }
        return num;
    }

    @Override
    public int updateUser(AddUser addUser) {
        //要先删除角色信息,再添加角色信息

        User user = new User();
        user.setId(addUser.getId()+"");
        user.setUsername(addUser.getUsername());
        user.setEmail(addUser.getEmail());
        user.setPassword(addUser.getPassword());
        user.setRemark(addUser.getRemark());
        int num = mapper.updateUser(user);
        mapper.deleteRole(user.getId());
        if (addUser.getRoleIds().size()!= 0 && addUser.getRoleIds()!= null){
            for (String roleId : addUser.getRoleIds()) {
                mapper.addRole(roleId,user.getId());
            }
        }
        return num;
    }

    @Override
    public int deleteById(int id) {
        mapper.deleteRole(id+"");
       int num =  mapper.deleteUserByPage(id+"");
        return num;
    }

    @Override
    public void add() {
        System.out.println("方法执行了");
    }


}

